/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package uloha4.view;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import javax.swing.*;
import javax.swing.table.TableCellRenderer;
import javax.swing.table.TableColumn;
import uloha4.controller.AppController;
import uloha4.controller.ScaleController;
import uloha4.controller.TranslateController;
import uloha4.model.ComponentTableModel;

/**
 *
 * @author Tomique
 */
public class View {

    private MagicCanvas canvas;
    private JPanel settingsPanel;
    private ComponentTableModel tableModel;

    public View() {
        initCanvas();
        initComponents();
        initFrame();
    }

    private void initCanvas() {

        canvas = MagicCanvas.getInstance();
        canvas.setPreferredSize(new Dimension(640, 480));

        // inicializuji controllery
        TranslateController tc = new TranslateController();
        ScaleController sc = new ScaleController();

        // priradim controllery
        canvas.addMouseListener(tc);
        canvas.addMouseMotionListener(tc);
        canvas.addMouseWheelListener(sc);
    }

    private void initComponents() {
        final int PREFFERED_WIDTH = 250;
        settingsPanel = new JPanel();
        settingsPanel.setLayout(new BoxLayout(settingsPanel, BoxLayout.Y_AXIS));
        settingsPanel.setPreferredSize(new Dimension(PREFFERED_WIDTH, 0));
        settingsPanel.setMaximumSize(new Dimension(PREFFERED_WIDTH, 10000));
        settingsPanel.setBorder(BorderFactory.createEmptyBorder(10, 10, 10, 10));

        // nastaveni vykreslovani
        JPanel appPanel = new JPanel();
        appPanel.setMaximumSize(new Dimension(250, 100));
        appPanel.setLayout(new GridLayout(5, 1));


        // inicializace controlleru
        AppController ac = new AppController(this);

        // antialias
        JCheckBox antialiasCheck = new JCheckBox("Antialiasing");
        antialiasCheck.addActionListener(ac);
        appPanel.add(antialiasCheck);

        // jmena uzlu
        JCheckBox nodeNameCheck = new JCheckBox("Named nodes");
        nodeNameCheck.addActionListener(ac);
        appPanel.add(nodeNameCheck);

        // orientace hran
        JCheckBox forceOrietCheck = new JCheckBox("Force orientation");
        forceOrietCheck.addActionListener(ac);
        appPanel.add(forceOrietCheck);
        
        // vizualizace osamocenych uzlu
        JCheckBox lonelyNodeCheck = new JCheckBox("Visualize lonely nodes");
        lonelyNodeCheck.addActionListener(ac);
        appPanel.add(lonelyNodeCheck);
        
        // vizualizace stupne uzlu
        JCheckBox degreeCheck = new JCheckBox("Visualize node degree");
        degreeCheck.addActionListener(ac);
        appPanel.add(degreeCheck);

        appPanel.setBorder(BorderFactory.createTitledBorder("Visualization parameters"));

        // vyobrazeni jednotlivych komponent v tabulce
        JPanel tablePanel = new JPanel(new GridLayout(1, 1));

        // tabulka se sloupci, zarovnanymi na stred
        tableModel = new ComponentTableModel();
        JTable table = new JTable(tableModel) {

            @Override
            public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
                Component comp = super.prepareRenderer(renderer, row, column);

                if (comp instanceof JLabel) {
                    ((JLabel) comp).setHorizontalAlignment(JLabel.CENTER);
                }
                return comp;
            }
        };              
        setupTableColumnWidth(table);
        table.setFillsViewportHeight(true);
        table.setAutoCreateRowSorter(true);

        JScrollPane tablePane = new JScrollPane(table);
        tablePanel.add(tablePane);
        
        // manipulace s oznacenymi radky
        JPanel selectionPanel = new JPanel();
        selectionPanel.setLayout(new BoxLayout(selectionPanel, BoxLayout.X_AXIS));
        selectionPanel.setBorder(BorderFactory.createEmptyBorder(10, 0, 5, 0));
        
        // all
        JButton allButton = new JButton("All");      
        allButton.addActionListener(ac);
        selectionPanel.add(allButton);
        
        // none
        JButton noneButton = new JButton("None");
        noneButton.addActionListener(ac);
        selectionPanel.add(noneButton);
        
        // inverse
        JButton inverseButton = new JButton("Inverse");
        inverseButton.addActionListener(ac);
        selectionPanel.add(inverseButton);
        selectionPanel.add(Box.createHorizontalGlue());
        
        settingsPanel.add(appPanel);
        settingsPanel.add(selectionPanel);
        settingsPanel.add(tablePanel);
        
    }

    /**
     * Nastavi sirku jednotlivych sloupcu tabulky
     *
     * @param table
     */
    private void setupTableColumnWidth(JTable table) {
        // <editor-fold defaultstate="collapsed" desc="setupTableColumnWidth() implementation">
        TableColumn column;
        for (int i = 0; i < 4; i++) {
            column = table.getColumnModel().getColumn(i);
            switch (i) {
                case 0:
                    column.setPreferredWidth(30);
                    break;
                case 1:
                    column.setPreferredWidth(100);
                    break;
            }
        }
        // </editor-fold>
    }
    
    public void updateTable() {
        tableModel.fireTableDataChanged();
    }

    private void initFrame() {

        JFrame frame = new JFrame("VIZ - 4");
        frame.getContentPane().setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.X_AXIS));
        frame.getContentPane().add(canvas);
        frame.getContentPane().add(settingsPanel);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }
}
